//
//  RWHud.m
//  Alumna
//
//  Created by ray on 2021/11/17.
//

#import "RWHUD.h"
#import "MBProgressHUD.h"

void HUDMASYNC(dispatch_block_t block) {
    if (dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL) == dispatch_queue_get_label(dispatch_get_main_queue())) {
        block();
    } else {
        dispatch_async(dispatch_get_main_queue(), block);
    }
}

static MBProgressHUD * _hud = nil;

@implementation RWHUD

+ (void)showMessage:(NSString *)text inView:(UIView *)view {
    HUDMASYNC(^{
        MBProgressHUD *hud = [self p_mbProgressHUDInView:view];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = text;
        [hud hideAnimated:YES afterDelay:2.f];
    });
}

+ (void)showSuccess:(NSString *)text inView:(UIView *)view {
    HUDMASYNC(^{
        MBProgressHUD *hud = [self p_mbProgressHUDInView:view];
        hud.mode = MBProgressHUDModeCustomView;
        UIImage *image = [[UIImage imageNamed:@"rw_hud_success"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        hud.customView = [[UIImageView alloc] initWithImage:image];
        hud.square = YES;
        hud.label.text = text;
        [hud hideAnimated:YES afterDelay:1.5];
    });
}

+ (void)showFail:(NSString *)text inView:(UIView *)view {
    [self showMessage:text inView:view];
    
    #warning 临时取消错误弹窗（后期按设计图优化）
    //    HUDMASYNC(^{
    //        MBProgressHUD *hud = [self p_mbProgressHUD];
    //        hud.mode = MBProgressHUDModeCustomView;
    //        UIImage *image = [[UIImage imageNamed:@"rw_hud_error"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    //        hud.customView = [[UIImageView alloc] initWithImage:image];
    //        hud.square = YES;
    //        hud.label.text = text;
    //        [hud hideAnimated:YES afterDelay:1.5];
    //    });
}

+ (void)showWarning:(NSString *)text inView:(UIView *)view {
    HUDMASYNC(^{
        MBProgressHUD *hud = [self p_mbProgressHUDInView:view];
        hud.mode = MBProgressHUDModeCustomView;
        UIImage *image = [[UIImage imageNamed:@"rw_hud_warning"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        hud.customView = [[UIImageView alloc] initWithImage:image];
        hud.square = YES;
        hud.label.text = text;
        [hud hideAnimated:YES afterDelay:1.5];
    });
}

+ (void)showMessage:(NSString *)text {
    UIView *view = [self _mbProgressHUDDefaultParentView];
    [self showMessage:text inView:view];
}

+ (void)showSuccess:(NSString *)text {
    UIView *view = [self _mbProgressHUDDefaultParentView];
    [self showSuccess:text inView:view];
}

+ (void)showFail:(NSString *)text {
    UIView *view = [self _mbProgressHUDDefaultParentView];
    [self showFail:text inView:view];
}

+ (void)showWarning:(NSString *)text {
    UIView *view = [self _mbProgressHUDDefaultParentView];
    [self showWarning:text inView:view];
}

+ (void)showLoading {
    if (_hud) {
        return;
    }
    
    HUDMASYNC(^{
        MBProgressHUD *hud = [self p_mbProgressHUD];
        _hud = hud;
    });
}

+ (void)hiddenLoading {
    
    HUDMASYNC(^{
        [_hud hideAnimated:YES];
        _hud = nil;
    });
}

+ (void)showLoadingInView:(UIView *)view {
    HUDMASYNC(^{
        [self p_mbProgressHUDInView:view];
    });
}

+ (void)hiddenLoadingInView:(UIView *)view {
    HUDMASYNC(^{
        for (UIView *subView in view.subviews) {
            if ([subView isKindOfClass:MBProgressHUD.class]) {
                MBProgressHUD *hud = (MBProgressHUD *)subView;
                [hud hideAnimated:YES];
            }
        }
    });
}

#pragma mark - *****************************************  Private  *********************************************

+ (UIView *)_mbProgressHUDDefaultParentView {
    return [[self class] p_currentWindow];
}

+ (MBProgressHUD *)p_mbProgressHUDInView:(UIView *)view {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    //修改样式，否则等待框背景色将为半透明
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.color = [UIColor colorWithWhite:0 alpha:0.7];
    hud.contentColor = [UIColor whiteColor];
    return hud;
}

+ (MBProgressHUD *)p_mbProgressHUD {
    UIView *view = [self _mbProgressHUDDefaultParentView];
    return [self p_mbProgressHUDInView:view];
}

+ (UIWindow *)p_currentWindow {
    
    id appDelegate = [UIApplication sharedApplication].delegate;
    if (appDelegate && [appDelegate respondsToSelector:@selector(window)]) {
        return [appDelegate window];
    }
    
    if (@available(iOS 15, *)) {
       __block UIScene * _Nonnull tmpSc;
        [[[UIApplication sharedApplication] connectedScenes] enumerateObjectsUsingBlock:^(UIScene * _Nonnull obj, BOOL * _Nonnull stop) {
            if (obj.activationState == UISceneActivationStateForegroundActive || obj.activationState == UISceneActivationStateForegroundInactive) {
                tmpSc = obj;
                *stop = YES;
            }
        }];
        UIWindowScene *curWinSc = (UIWindowScene *)tmpSc;
        return curWinSc.keyWindow;
    } else {
        
        NSArray *windows = [UIApplication sharedApplication].windows;
        
        if  (windows > 0) {
            return windows.firstObject;
        }
        return nil;
    }
}

@end
